package com.geek.novel.theme

import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.text.TextUtils
import android.util.AttributeSet
import android.view.View
import com.geek.novel.R
import com.geek.novel.utils.CommUtil
import com.geek.novel.utils.LogCatUtil
import skin.support.SkinCompatManager
import skin.support.observe.SkinObserver

/**基本主题，所有子主题都基础这个类*/
abstract class BaseTheme : View {

    /**主题基色*/
    abstract fun themeColor(): Int

    /**主题名称*/
    abstract fun themeName(): String

    /**主题边框颜色*/
    open fun borderColor(): Int = Color.WHITE
    open fun borderActiveColor(): Int = context.getColor(R.color.book_dialog_theme_border)


    /**默认主题颜色*/
    val defaultTheme = "default"

    /**是否选中当前主题*/
    private var isChecked = false

    /**边距*/
    private var padding = CommUtil.dip2px(5F)

    private lateinit var paint: Paint


    constructor(context: Context): super(context){
        this.init(context)
    }

    constructor(context: Context, attrs: AttributeSet): super(context, attrs){
        this.init(context)
    }

    constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int): super(context, attrs, defStyleAttr){
        this.init(context)
    }


    open fun setChecked(){
        var curSkinName = SkinCompatManager.getInstance().curSkinName
        if(TextUtils.isEmpty(curSkinName)){
            curSkinName = defaultTheme
        }
        this.isChecked = curSkinName == themeName()
    }

    /**切换主题*/
    fun toggleTheme(){
        val themeName = themeName()
        if(defaultTheme == themeName){
            //恢复默认皮肤
            SkinCompatManager.getInstance().restoreDefaultTheme()
        } else{
            //切换皮肤名为night的皮肤
            SkinCompatManager.getInstance().loadSkin(themeName, null, SkinCompatManager.SKIN_LOADER_STRATEGY_BUILD_IN)
        }
    }


    open fun onDestroy(){
        SkinCompatManager.getInstance().deleteObserver(skinObserver)
    }


    /**换肤监听*/
    private val skinObserver = SkinObserver { _, _ ->
        this.setChecked()
        this.postInvalidate()
    }

    private fun init(context: Context){
        this.setChecked()

        SkinCompatManager.getInstance().addObserver(skinObserver)

        paint = Paint()
        paint.isAntiAlias = true    //抗锯齿
        paint.strokeWidth = CommUtil.dip2px(2F)
    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, widthMeasureSpec)
    }

    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)

        val w = width
        if(w == 0){
            return
        }

        val x = w.toFloat() / 2

        //绘制圆圈
        paint.color = if(this.isChecked){
            borderActiveColor()
        }else{
            borderColor()
        }
        val borderWidth = (w / 2).toFloat() - padding*2
        canvas!!.drawCircle(x, x, borderWidth, paint)

        //绘制中心圆
        paint.color = this.themeColor()
        val circleWidth = (w / 2).toFloat() - paint.strokeWidth - padding*2
        canvas.drawCircle(x, x, circleWidth, paint)
    }

}